package List排序;

import java.time.LocalDate;
import java.util.*;

public class 其他排序
{
    public static void main(String[] args) {
        // 创建可变的员工列表
        List<Employee> employees = new ArrayList<>(Arrays.asList(
            new Employee("E1003", "王五", "技术部", LocalDate.of(2021, 7, 22), 18000, 3),
            new Employee("E1001", "张三", "技术部", LocalDate.of(2020, 3, 15), 15000, 4),
            new Employee("E1005", "钱七", "技术部", LocalDate.of(2020, 3, 15), 15000, 5),
            new Employee("E1002", "李四", "市场部", LocalDate.of(2022, 1, 10), 12000, 5),
            new Employee("E1004", "赵六", "财务部", LocalDate.of(2019, 5, 30), 16000, 4)
        ));

        System.out.println("原始员工列表：");
        printEmployees(employees);
        System.out.println("\n" + "=".repeat(60) + "\n");

        // 1. 使用Comparable接口（自然排序）
        System.out.println("1. 使用Comparable接口（按ID排序）:");
        Collections.sort(employees); // 自然排序
        printEmployees(employees);

        // 2. 使用Comparator匿名内部类
        System.out.println("\n2. 使用Comparator匿名内部类（按薪资降序）:");
        Collections.sort(employees, new Comparator<Employee>() {
            @Override
            public int compare(Employee e1, Employee e2) {
                return Double.compare(e2.getSalary(), e1.getSalary());
            }
        });
        printEmployees(employees);

        // 3. 使用Lambda表达式
        System.out.println("\n3. 使用Lambda表达式（按绩效降序）:");
        employees.sort((e1, e2) -> e2.getPerformance() - e1.getPerformance());
        printEmployees(employees);

        // 4. 使用方法引用和Comparator.comparing
        System.out.println("\n4. 使用方法引用（按入职日期升序）:");
        employees.sort(Comparator.comparing(Employee::getHireDate));
        printEmployees(employees);

        // 5. 使用链式Comparator
        System.out.println("\n5. 使用链式Comparator（部门升序 > 薪资降序 > 绩效降序）:");
        employees.sort(Comparator
            .comparing(Employee::getDepartment)
            .thenComparing(Employee::getSalary, Comparator.reverseOrder())
            .thenComparing(Employee::getPerformance, Comparator.reverseOrder())
        );
        printEmployees(employees);

        // 6. 使用自定义Comparator类
        System.out.println("\n6. 使用自定义Comparator类（按工龄降序）:");
        employees.sort(new SeniorityComparator());
        printEmployees(employees);

        // 7. 处理空值的安全排序
        System.out.println("\n7. 处理空值的安全排序（部门可能为null）:");
        employees.add(new Employee("E1006", "孙八", null, LocalDate.now(), 14000, 4));
        employees.sort(Comparator
            .comparing(Employee::getDepartment, 
                Comparator.nullsLast(String::compareTo))
            .thenComparing(Employee::getName)
        );
        printEmployees(employees);

        // 8. 使用Stream API进行排序（不影响原始列表）
        System.out.println("\n8. 使用Stream API排序（按薪资升序）:");
        employees.stream()
            .sorted(Comparator.comparingDouble(Employee::getSalary))
            .forEach(System.out::println);
    }

    private static void printEmployees(List<Employee> employees) {
        System.out.println("ID\t姓名\t部门\t\t入职日期\t\t薪资\t绩效");
        System.out.println("------------------------------------------------------------");
        employees.forEach(e -> System.out.printf("%s\t%s\t%-8s\t%s\t%.0f\t%d%n",
            e.getId(), e.getName(), 
            e.getDepartment() != null ? e.getDepartment() : "未分配",
            e.getHireDate(), e.getSalary(), e.getPerformance()));
    }
}

// 员工类实现Comparable接口
class Employee implements Comparable<Employee> {
    private String id;
    private String name;
    private String department;
    private LocalDate hireDate;
    private double salary;
    private int performance;

    public Employee(String id, String name, String department, 
                    LocalDate hireDate, double salary, int performance) {
        this.id = id;
        this.name = name;
        this.department = department;
        this.hireDate = hireDate;
        this.salary = salary;
        this.performance = performance;
    }

    // 实现Comparable接口，按ID排序
    @Override
    public int compareTo(Employee other) {
        return this.id.compareTo(other.id);
    }

    // Getter方法
    public String getId() { return id; }
    public String getName() { return name; }
    public String getDepartment() { return department; }
    public LocalDate getHireDate() { return hireDate; }
    public double getSalary() { return salary; }
    public int getPerformance() { return performance; }

    @Override
    public String toString() {
        return String.format("%s: %s (%s) | 入职: %s | 薪资: %.0f | 绩效: %d",
                id, name, department != null ? department : "未分配", 
                hireDate, salary, performance);
    }
}

// 自定义Comparator类：按工龄降序排序
class SeniorityComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee e1, Employee e2) {
        // 计算工龄（以年为单位）
        long years1 = e1.getHireDate().until(LocalDate.now()).getYears();
        long years2 = e2.getHireDate().until(LocalDate.now()).getYears();
        
        // 降序排列（工龄长的在前）
        return Long.compare(years2, years1);
    }
}